<!--
 * @Description:退款管理
-->
<template>
  <div class="registered-container refund appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">昨日</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">近7天</el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">近一个月</el-button>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item label="平台订单号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入订单号" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <el-table-column type="index" width="90" label="序号" />
        <el-table-column label="订单号" align="center">
          <template slot-scope="scope">{{ scope.row.self_order_no||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易流水号" align="center">
          <template slot-scope="scope">{{ scope.row.transcation_id||'-' }}</template>
        </el-table-column>
        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">{{ scope.row.bill_type_name||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易金额" align="center">
          <template slot-scope="scope">{{ scope.row.pay_fee||'-' }}</template>
        </el-table-column>
        <el-table-column label="退款金额" align="center">
          <template slot-scope="scope">{{ scope.row.pay_fee||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope">{{ scope.row.pay_style_name||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易状态" align="center">
          <template slot-scope="scope">{{ scope.row.status||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">{{ scope.row.trans_time||'-' }}</template>
        </el-table-column>
        <el-table-column label="退款时间" align="center">
          <template slot-scope="scope">{{ scope.row.self_no||'-' }}</template>
        </el-table-column>

      </el-table>
    </template>

    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType, payRecord } from '@/api/transactRecords/index'
import Pagination from '@/components/Pagination' // 分页
export default {
  name: 'Refund',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 1, // 按天月年
      searchKey: {
        order_no: '', // 订单号
        time: '', // 账单日期
        pay_style: '', // 支付方式
        PayStyle: []
      },

      searchDate: [{}],

      addModel: {
        flag: false,
        item: {
          id: '',
          name: '', // 角色名称
          status: 1 // 1:启用 0:禁用
        }
      },

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: { // 日期不能点击
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      }
    }
  },

  created() {
    this.searchKey.time = this.utils.Foryest()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        var defaultValue = [{ id: '', no: '', name: '全部' }]
        // 支付方式
        var tempPay = res.data.PayStyle
        var PayStyle = [...defaultValue, ...tempPay]
        that.searchKey.PayStyle = PayStyle
      })
        .catch(() => {
        })
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date_type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.start_time = that.searchKey.time[0]
        params.end_time = that.searchKey.time[1]
      } else {
        params.start_time = ''
        params.end_time = ''
      }
      params.order_no = that.searchKey.order_no
      params.pay_style = that.searchKey.pay_style
      that.page.tableLoading = true
      payRecord(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.page.totalPage = res.data.pageSum
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date_type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.start_time = that.searchKey.time[0]
        params.end_time = that.searchKey.time[1]
      } else {
        params.start_time = ''
        params.end_time = ''
      }
      params.order_no = that.searchKey.order_no
      params.pay_style = that.searchKey.pay_style
      params.export_flag = 1
      var viewUrl = `/ApiAdmin/Record/payRecord?&page=${params.page}&limit=${params.limit}&date_type=${
        params.date_type
      }&order_no=${
        params.order_no
      }&start_time=${
        params.start_time
      }&end_time=${params.end_time}&pay_style=${params.pay_style}&export_flag=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },

    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 1
        this.searchKey.time = this.utils.Foryest()

        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 2
        this.searchKey.time = this.utils.Forseven()

        this.onLoad()
      } else {
        this.date = 3
        this.searchKey.time = this.utils.Formonth()

        this.onLoad()
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.refund {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
}
</style>
